package gui.addrecipe;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import start.Start;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

/**
 * Unterpanel von AddRecipe.java.
 * Enthaelt Liste von Zutaten fuer das Rezept.
 * @author Dominik Korittki
 */
public class AddRecipeRightTopPanel extends JPanel {
	
	/** Serial Version ID */
	private static final long serialVersionUID = 1L;
	
	/** Speichert die Zutaten in einem Modell */
	private DefaultListModel model;
	
	/** Visuelle Liste welches den Inhalt des Modells ausgibt */
	private JList list;
	
	/** Scrollfenster fuer die Liste */
	private JScrollPane scrollPane;
	
	/** Loeschbutton fuer die Zutatenliste */
	private JButton btnRemove;
	
	
	/**
	 * Erstellt das Panel
	 */
	public AddRecipeRightTopPanel() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0};
		gridBagLayout.rowHeights = new int[]{120, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{3.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		model = new DefaultListModel();
		
		list = new JList(model);
		list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		list.setLayoutOrientation(JList.VERTICAL);
		
		
		scrollPane = new JScrollPane(list);
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setPreferredSize(new Dimension());
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 0;
		add(scrollPane, gbc_scrollPane);
		
		ImageIcon iconRemove = new ImageIcon(Start.getImgs().getMinus(), "Zutat löschen");
		btnRemove = new JButton("Löschen", iconRemove);
		btnRemove.setToolTipText("Lösche Ausgewähltes Rezept aus Liste");
		btnRemove.addActionListener(new ActionListener() {
			/*
			 * Loescht alle selektierten Elemente aus dem Modell bzw. der Liste
			 */
			@Override public void actionPerformed(ActionEvent arg0) {
				if(list.getSelectedIndex() >= 0) model.removeRange(
						list.getSelectedIndices()[0], //erstes Element ..
						list.getSelectedIndices()[list.getSelectedIndices().length-1]); //.. bis letztes Element
			}
		});
		btnRemove.addKeyListener(new KeyListener() {
			@Override public void keyPressed(KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER) 
						if(list.getSelectedIndex() >= 0) 
							model.removeRange(
								list.getSelectedIndices()[0], //erstes Element ..
								list.getSelectedIndices()[list.getSelectedIndices().length-1]	//.. bis letztes Element
							); 
			}
			@Override public void keyReleased(KeyEvent e) {}
			@Override public void keyTyped(KeyEvent e) {}
		});
		GridBagConstraints gbc_lblRemove = new GridBagConstraints();
		gbc_lblRemove.insets = new Insets(0, 0, 5, 0);
		gbc_lblRemove.fill = GridBagConstraints.NONE;
		gbc_lblRemove.gridx = 0;
		gbc_lblRemove.gridy = 1;
		add(btnRemove, gbc_lblRemove);
	}
	
	/**
	 * Fuegt ein String Objekt ans Ende der JList hinzu
	 * @param arg0 String, welcher hinzugefuegt werden soll
	 */
	public void addListObject(String arg0) {
		model.addElement(arg0);
	}
	
	/**
	 * Getter fuer die Zutatenliste als Arraylist
	 * @return Arraylist<String> fuer die Zutatenliste
	 */
	public ArrayList<String> getZutaten() {
		ArrayList<String> output = new ArrayList<String>();
		for(int i=0; i<model.getSize(); i++) {
			output.add((String) model.get(i));
		}
		return output;
	}
	
	/**
	 * Loeschen aller Zutaten aus der Liste
	 */
	public void reset() {
		model.removeAllElements();
	}

	/**
	 * Getter Methode fuer das ListModell, welches die Zutaten speichert
	 * @return ListModell, welches die Zutaten speichert
	 */
	public DefaultListModel getModel() {
		return model;
	}
	
}
